package thss.mro.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import thss.mro.entity.DynamicAttrs;
import thss.mro.entity.TemplateField;
import thss.mro.entity.template.ProtocolHeadTemplate;
import thss.mro.entity.template.ProtocolTemplate;
import thss.mro.manager.TemplateManager;
import thss.mro.parser.BitPacketParser;
import thss.mro.parser.BytePacketParser;
import thss.mro.parser.IPacketParser;

/**
 * 模板资源类，定义了所有协议的头部和正文的模板
 * 
 * @author zhuangxy
 * 
 */
public class Template {

	private static HashMap<String, ProtocolTemplate> templates;
	
	private static HashMap<String, IPacketParser> parsers; //模板与解析器映射：key-模板ID,value-解析器
	
	public static List<Integer> SCPCellCodeLst = new ArrayList<Integer>(){{
		add(10001);
		add(10002);
		add(10003);
	}};
	
	static {
		new Template();
	}

	public Template() {
		templates = new HashMap<String, ProtocolTemplate>();
		parsers = new HashMap<String, IPacketParser>();
		BytePacketParser bytePacketParser = new BytePacketParser();
		BitPacketParser bitPacketParser = new BitPacketParser();
		//初始化SCP头部
		ProtocolHeadTemplate scpHead = new ProtocolHeadTemplate();
		scpHead.setTemplateId(Constants.TEMPLATE_ID_SCP_HEAD);
		scpHead.setProtocolName(Constants.PROTOCOL_NAME_SCP);
		scpHead.setValueType(Constants.PROTOCOL_VALUE_TYPE_BYTE);
		TemplateField companyName = new TemplateField("CompanyName",2,"公司名",Constants.PROTOCOL_FIELD_TYPE_CHAR);
		TemplateField protocolType = new TemplateField("ProtocolType",3,"协议类型,",Constants.PROTOCOL_FIELD_TYPE_BYTE);
		TemplateField symt = new TemplateField("SYMT",4,"SYMT编号",Constants.PROTOCOL_FIELD_TYPE_BYTE);
		TemplateField packetLength = new TemplateField("PacketLength",3,"包长度",Constants.PROTOCOL_FIELD_TYPE_BYTE);
		scpHead.addTemplateField(companyName);
		scpHead.addTemplateField(protocolType);
		scpHead.addTemplateField(symt);
		scpHead.addTemplateField(packetLength);
		templates.put(scpHead.getTemplateId(), scpHead);
		parsers.put(scpHead.getTemplateId(), bytePacketParser);
		//初始化SCPCell头部
		ProtocolHeadTemplate scpCellHead = new ProtocolHeadTemplate();
		scpCellHead.setTemplateId(Constants.TEMPLATE_ID_SCPCELL_HEAD);
		scpCellHead.setProtocolName(Constants.PROTOCOL_NAME_SCPCELL);
		scpCellHead.setValueType(Constants.PROTOCOL_VALUE_TYPE_BYTE);
		TemplateField cellCode = new TemplateField("cellCode",4,"信元编码",Constants.PROTOCOL_FIELD_TYPE_BYTE);
		scpCellHead.addTemplateField(cellCode);
		templates.put(scpCellHead.getTemplateId(), scpCellHead);
		parsers.put(scpCellHead.getTemplateId(), bytePacketParser);
		//初始化SCP正文
		ProtocolTemplate scpBody = new ProtocolTemplate();
		scpBody.setTemplateId(Constants.TEMPLATE_ID_SCP_BODY);
		scpBody.setValueType(Constants.PROTOCOL_VALUE_TYPE_BYTE);
		DynamicAttrs da = new DynamicAttrs(2);//设置信元字段的动态属性(末端偏移为2)
		TemplateField cell = new TemplateField("cell",12,"信元",Constants.PROTOCOL_FIELD_TYPE_BYTE,da);
		TemplateField checkCode = new TemplateField("checkCode",2,"校验码",Constants.PROTOCOL_FIELD_TYPE_BYTE);
		scpBody.addTemplateField(cell);
		scpBody.addTemplateField(checkCode);
		templates.put(scpBody.getTemplateId(), scpBody);
		parsers.put(scpBody.getTemplateId(), bytePacketParser);
		//初始化SCPCell正文
		ProtocolTemplate scpCellBody1 = new ProtocolTemplate();
		scpCellBody1.setTemplateId(Constants.TEMPLATE_ID_SCPCELL_BODY+SCPCellCodeLst.get(0));
		scpCellBody1.setValueType(Constants.PROTOCOL_VALUE_TYPE_BYTE);
		TemplateField f11 = new TemplateField("cell_1_1(2B)",2,"值1",Constants.PROTOCOL_FIELD_TYPE_CHAR);
		TemplateField f12 = new TemplateField("cell_1_2(3B)",3,"值2",Constants.PROTOCOL_FIELD_TYPE_CHAR);
		TemplateField f13 = new TemplateField("cell_1_3(3B)",3,"值3",Constants.PROTOCOL_FIELD_TYPE_BYTE);
		scpCellBody1.addTemplateField(f11);
		scpCellBody1.addTemplateField(f12);
		scpCellBody1.addTemplateField(f13);
		ProtocolTemplate scpCellBody2 = new ProtocolTemplate();
		scpCellBody2.setTemplateId(Constants.TEMPLATE_ID_SCPCELL_BODY+SCPCellCodeLst.get(1));
		scpCellBody2.setValueType(Constants.PROTOCOL_VALUE_TYPE_BYTE);
		TemplateField f21 = new TemplateField("cell_2_1(1B)",1,"值1",Constants.PROTOCOL_FIELD_TYPE_BYTE);
		TemplateField f22 = new TemplateField("cell_2_2(7B)",7,"值2",Constants.PROTOCOL_FIELD_TYPE_CHAR);
		scpCellBody2.addTemplateField(f21);
		scpCellBody2.addTemplateField(f22);
		ProtocolTemplate scpCellBody3 = new ProtocolTemplate();
		scpCellBody3.setTemplateId(Constants.TEMPLATE_ID_SCPCELL_BODY+SCPCellCodeLst.get(2));
		scpCellBody3.setValueType(Constants.PROTOCOL_VALUE_TYPE_BIT);
		for(int i=0;i<64;i++){
			TemplateField f = new TemplateField("cell_3_"+i,1,"位1",Constants.PROTOCOL_FIELD_TYPE_BIT);
			scpCellBody3.addTemplateField(f);
		}
		templates.put(scpCellBody1.getTemplateId(), scpCellBody1);
		parsers.put(scpCellBody1.getTemplateId(), bytePacketParser);
		templates.put(scpCellBody2.getTemplateId(), scpCellBody2);
		parsers.put(scpCellBody2.getTemplateId(), bytePacketParser);
		templates.put(scpCellBody3.getTemplateId(), scpCellBody3);
		parsers.put(scpCellBody3.getTemplateId(), bitPacketParser);//位解析
	}

	public static ProtocolTemplate getTemplate(String templateId) {
		return templates.get(templateId);
	}
	
	public static IPacketParser getParser(String templateId) {
		return parsers.get(templateId);
	}

	public static HashMap getAllTemplates() {
		return templates;
	}
	
	public static void main(String[] args){
//		ProtocolTemplate pt = Template.getTemplate("SCPHead");
//		ProtocolHeadTemplate pht = (ProtocolHeadTemplate)pt;
		ProtocolHeadTemplate pht = TemplateManager.getHeadTemplate("SCPHead");
		System.out.println(pht.getProtocolName());
	}

}
